package com.hymjweb.demo.hymj.out_unit_manage.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.out_unit_manage.bean.BOutUnitPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface BOutUnitMapper {

    class BOutUnitMapperProvider{

        public String findAll(Map map) {

            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_out_unit t where 1 = 1 ");
            }}.toString();

            if (map != null) {
                String name = DataTypeUtil.toNotNullString(map.get("name"));
                String address = DataTypeUtil.toNotNullString(map.get("address"));

                if(!DataTypeUtil.isEmptyStr(name)){
                    sql += " and t.name like " + DataTypeUtil.quoteAllLikeStr(name);
                }

                if(!DataTypeUtil.isEmptyStr(address)){
                    sql += " and t.address like " + DataTypeUtil.quoteAllLikeStr(address);
                }

            }
            return  sql;
        }


        public String findCacheAll() {
            return new SQL() {{
                SELECT("t.* ");
                FROM("b_out_unit t ");
            }}.toString();
        }

        public String insert(BOutUnitPojo pojo) {
            String sql = new SQL() {{
                INSERT_INTO("b_out_unit");
                VALUES("id","#{id}");
                VALUES("name","#{name}");
                VALUES("contact","#{contact}");
                VALUES("phone","#{phone}");
                VALUES("address","#{address}");
                VALUES("comment","#{comment}");
                VALUES("master_bill_foreign_company_id","#{masterBillForeignCompanyId}");
            }}.toString();
            return sql;
        }

        public String update(BOutUnitPojo pojo) {
            String sql= new SQL() {{
                UPDATE("b_out_unit t");
                SET("t.name = #{name}");
                SET("t.contact = #{contact}");
                SET("t.phone = #{phone}");
                SET("t.address = #{address}");
                SET("t.comment = #{comment}");
                SET("t.master_bill_foreign_company_id = #{masterBillForeignCompanyId}");
                WHERE("t.id=#{id}");
            }}.toString();
            return sql;
        }

        public String delete(String id) {
            String sql = new SQL() {{
                DELETE_FROM("b_out_unit");
                WHERE("id = " + id );
            }}.toString();
            return sql;
        }

        public String findObjectById(String id) {
            return new SQL() {{
                SELECT("t.*");
                FROM("b_out_unit t");
                WHERE("t.id = " + id);
            }}.toString();
        }

        public String findUK(String name,String exceptId) {
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_out_unit t");
                WHERE(" t.name = " + DataTypeUtil.quoteStr(name));
            }}.toString();

            if(!DataTypeUtil.isEmptyStr(exceptId)){
                sql += " and t.id != " + DataTypeUtil.quoteStr(exceptId);
            }

            return  sql;
        }

    }

    @Results(id = "bOutUnitMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "contact", property = "contact"),
            @Result(column = "phone", property = "phone"),
            @Result(column = "address", property = "address"),
            @Result(column = "comment", property = "comment"),
            @Result(column = "master_bill_foreign_company_id", property = "masterBillForeignCompanyId"),
    })
    @SelectProvider(type = BOutUnitMapperProvider.class, method = "findAll")
    List<BOutUnitPojo> findAll(Map map);

    @ResultMap("bOutUnitMapId")
    @SelectProvider(type = BOutUnitMapperProvider.class, method = "findCacheAll")
    List<BOutUnitPojo> findCacheAll();

    @ResultMap(value = "bOutUnitMapId")
    @SelectProvider(type = BOutUnitMapperProvider.class, method = "findObjectById")
    BOutUnitPojo findObjectById(@Param("id") String id);

    @Results(id = "bOutUnitFindUKMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
    })
    @SelectProvider(type = BOutUnitMapperProvider.class, method = "findUK")
    BOutUnitPojo findUK(@Param("name") String name, @Param("exceptId") String exceptId);

    @InsertProvider(type = BOutUnitMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(BOutUnitPojo obj);

    @UpdateProvider(type = BOutUnitMapperProvider.class, method = "update")
    int update(BOutUnitPojo obj);

    @DeleteProvider(type = BOutUnitMapperProvider.class, method = "delete")
    int delete(String id);

}
